package com.laf.mall.api.repository;

import com.laf.mall.api.dto.MemberCar;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author ygl
 * @create 2018-12-26
 * @DESCRIPTION
 **/
@Repository
@Slf4j
public class MemberCarRepository {

    @Autowired
    JdbcTemplate jdbcTemplate;

    /**
     * 通过 会员ID 查询 会员绑定的车辆列表
     * @param memberId
     * @return
     */
    public List<MemberCar> findByMemberId(Integer memberId){
        String sql = "SELECT * FROM t_member_car WHERE member_id = ?";
        List<MemberCar> list = jdbcTemplate.query(sql,new Object[]{memberId},new MemberCar());
        return list;
    }


    /**
     * 通过 车牌号 查询 会员绑定的车辆列表
     * @param carNumber
     * @return
     */
    public List<MemberCar> findByCarNumber(String carNumber){
        String sql = "SELECT * FROM t_member_car WHERE car_number = ?";
        List<MemberCar> list = jdbcTemplate.query(sql,new Object[]{carNumber},new MemberCar());
        return list;
    }

    /**
     * 通过 会员ID 和 车牌号 删除数据
     * @param memberId
     * @param carNumber
     * @return
     */
    public int deleteByMemberIdAndCarNumber(Integer memberId,String carNumber){
        String sql = "DELETE FROM t_member_car WHERE member_id=? and car_number=?";
        return jdbcTemplate.update(sql,new Object[]{memberId,carNumber});
    }

    /**
     * 通过 会员ID 和 车牌号 新增
     * @param memberId
     * @param carNumber
     * @return
     */
    public int insert(Integer memberId,String carNumber){
        String sql = "INSERT INTO t_member_car(member_id,car_number) VALUES (?,?)";
        return jdbcTemplate.update(sql,new Object[]{memberId,carNumber});
    }

    public int deleteById(Integer id) {
        String sql = "DELETE FROM t_member_car WHERE id=?";
        return jdbcTemplate.update(sql,new Object[]{id});
    }
}
